package aboutme.felipesilvestre.oracaolista;

import java.util.ArrayList;
import java.util.HashMap;

import aboutme.felipesilvestre.oracaolista.util.CategoriaAdapter;
import aboutme.felipesilvestre.oracaolista.util.ListOracaoTesteUtil;
import aboutme.felipesilvestre.oracaolista.vo.Oracao;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;

public class OracaoActivity extends Activity {

	Oracao oracao;
	
	private int[] iconesCategorias = {R.drawable.categsaude, R.drawable.categfamilia, R.drawable.categfinanca,
									R.drawable.categrelacion,R.drawable.categorientacao,R.drawable.categministerio,R.drawable.categoutro};
	
	Spinner spin;
	EditText edTitulo;
	EditText edDesc;
	
	
	@Override
	public void onCreate(Bundle instace) {
		super.onCreate(instace);
		setContentView(R.layout.oracao);
		
		capturarIds();

		Bundle params = getIntent().getExtras();

		if (params != null) {

			boolean novo = params.getBoolean("novo");

			if (novo) {
				LinearLayout linCompartilhar = (LinearLayout) findViewById(R.id.linearCompartilhar);
				linCompartilhar.setVisibility(View.GONE);
				
				preencherSpinner();
				
				
			} else {
				Button btSave = (Button) findViewById(R.id.btSaveOracao);
				btSave.setVisibility(View.GONE);
				
				
				oracao = new Oracao(getBaseContext(), params.getInt("id"), params.getString("titulo"),params.getString("desc"), params.getInt("categoria"),0,"",params.getString("datacriacao"));
				
				
				preencherCampos();
				preencherSpinner();
				spin.setSelection(oracao.getCategoria());
				
			}
		}
		
		Button btSave = (Button) findViewById(R.id.btSaveOracao);
		btSave.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				oracao = new Oracao(getBaseContext(), 1, edTitulo.getText().toString().trim(), edDesc.getText().toString(), spin.getSelectedItemPosition(), 0, "","21/01/2012");
				ListOracaoTesteUtil.addOracao(oracao);
				finish();
			}
		});

	}

	private void preencherSpinner() {
		
		String[] nomeCategorias = {getText(R.string.saude).toString(),
				   getText(R.string.familia).toString(),
				   getText(R.string.financa).toString(),getText(R.string.relacionamento).toString(),
				   getText(R.string.orientacao).toString(),getText(R.string.ministerio).toString(),
				   getText(R.string.outro).toString()};
		
		
			ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
	        HashMap<String, Object> map;
	        
	        for(int i=0; i < nomeCategorias.length;i++){
	        	
	        	map = new HashMap<String, Object>();
	        
	        	map.put("Name", nomeCategorias[i]);
	        	map.put("Icon", iconesCategorias[i]);
	        	list.add(map);
	        }
	        
		        
	        CategoriaAdapter adapter = new CategoriaAdapter(getBaseContext(), list,
	                R.layout.spinnerlayout, new String[] { "Name", "Icon" },
	                new int[] { R.id.spNomeCateg, R.id.spImgCateg });
	
	        spin.setAdapter(adapter);
				
		
		
	}

	private void preencherCampos() {
							
		edTitulo.setText(oracao.getTitulo());
		edDesc.setText(oracao.getDescricao());
		
		edTitulo.setEnabled(false);
		edDesc.setEnabled(false);
		spin.setEnabled(false);
		
	}
	
	private void capturarIds(){
		spin = (Spinner) findViewById(R.id.spCategOracao);
		edTitulo = (EditText) findViewById(R.id.edTituloOracao);
		edDesc = (EditText) findViewById(R.id.edDescOracao);
				
		
	}

}
